package com.oracle.lingshangejia.pickerview;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.view.FullScreenDialog;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * --------------------------------------------
 * Create By :  Lvfq
 * Date ： 2016/8/25 0025 下午 5:50
 * -------------------------------------------
 **/
public class Util {

    /**
     * 时间选择回调
     */
    public interface TimerPickerCallBack {
        void onTimeSelect(String date);
    }

    /**
     * 弹出时间选择
     *
     * @param context
     * @param type     TimerPickerView 中定义的 选择时间类型
     * @param format   时间格式化
     * @param callBack 时间选择回调
     */
    public static void alertTimerPicker(Context context, TimePickerView.Type type, final String format, final TimerPickerCallBack callBack) {
        final TimePickerView pvTime = new TimePickerView(context, type);
        //控制时间范围
//        Calendar calendar = Calendar.getInstance();
//        pvTime.setRange(calendar.get(Calendar.YEAR) - 20, calendar.get(Calendar.YEAR));
        pvTime.setTime(new Date());
        pvTime.setCyclic(false);
        pvTime.setCancelable(true);
        pvTime.setTextSize(20);
        //弹出时间选择器
        pvTime.show();

        //时间选择后回调
        pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
//                        tvTime.setText(getTime(date));
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                callBack.onTimeSelect(sdf.format(date));
            }
        });
    }

    /**
     * 底部滚轮点击事件回调
     */
    public interface OnWheelViewClick {

        void onClick(View view, int postion);
    }

    public interface OnThreeWheelViewClick{

        void onThreeClick(View view, int postion, int position1, int position2);
    }

    private static List<String> list_day = new ArrayList<>();
    private static List<String> list_hour = new ArrayList<>();
    private static List<String> list_minute = new ArrayList<>();

    /**
     * 弹出底部滚轮选择
     */
    public static void alertBottomWheelOption(Context context, List<String> list1, List<String> list2, List<String> list3, final OnWheelViewClick click, final OnThreeWheelViewClick threeClick, int type) {

//        final PopupWindow popupWindow = new PopupWindow();

        final FullScreenDialog dialog = new FullScreenDialog(context);

        View view = null;
        final WheelView wv_option,wv_option_1,wv_option_2;
        TextView tv_cancel,tv_save;
        View tv_view;

        /**
         * type = 1 一个
         * type = 2 时间 天 小时  分
         * type = 3 三个
         */

        if (type == 1){

            view = LayoutInflater.from(context).inflate(R.layout.layout_bottom_wheel_option, null);
            wv_option = (WheelView) view.findViewById(R.id.wv_option);
            wv_option.setAdapter(new ArrayWheelAdapter((ArrayList) list1));
            //是否循环
            wv_option.setCyclic(false);
            wv_option.setCurrentItem(0);
            wv_option.setTextSize(21);

            tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
            tv_save = (TextView) view.findViewById(R.id.tv_save);
            tv_save.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    dialog.dismiss();
//                    popupWindow.dismiss();
                    click.onClick(v, wv_option.getCurrentItem());
                }
            });
            tv_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    dialog.dismiss();
//                    popupWindow.dismiss();
                }
            });
            tv_view = view.findViewById(R.id.tv_view);
            tv_view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    dialog.dismiss();
//                    popupWindow.dismiss();
                }
            });

        }else if (type == 2){

            for (int i = 0; i < 11; i++) {
                list_day.add(i+ "天");
            }
            for (int i = 0; i < 24; i++) {
                list_hour.add(i+ "小时");
            }
            for (int i = 0; i < 60; i++) {
                list_minute.add(i+ "分钟");
            }

            view = LayoutInflater.from(context).inflate(R.layout.layout_bottom_wheel_option_three_time, null);
            wv_option = (WheelView) view.findViewById(R.id.wv_option);
            wv_option_1 = (WheelView) view.findViewById(R.id.wv_option_1);
            wv_option_2 = (WheelView) view.findViewById(R.id.wv_option_2);
            wv_option.setAdapter(new ArrayWheelAdapter((ArrayList) list_day));
            wv_option_1.setAdapter(new ArrayWheelAdapter((ArrayList) list_hour));
            wv_option_2.setAdapter(new ArrayWheelAdapter((ArrayList) list_minute));
            //是否循环
            wv_option.setCyclic(false);
            wv_option.setCurrentItem(0);
            wv_option.setTextSize(21);
            wv_option_1.setCurrentItem(0);
            wv_option_1.setCyclic(false);
            wv_option_1.setTextSize(21);
            wv_option_2.setCyclic(false);
            wv_option_2.setCurrentItem(0);
            wv_option_2.setTextSize(21);
            view.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    int top = view.findViewById(R.id.ll_container).getTop();
                    if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                        int y = (int) motionEvent.getY();
                        if (y < top) {
                            dialog.dismiss();
//                            popupWindow.dismiss();
                            threeClick.onThreeClick(view,wv_option.getCurrentItem(),wv_option_1.getCurrentItem(),wv_option_2.getCurrentItem());

                            list_day.clear();
                            list_hour.clear();
                            list_minute.clear();

                        }
                    }
                    return true;
                }
            });
        }else if (type == 3){

            view = LayoutInflater.from(context).inflate(R.layout.layout_bottom_wheel_option_three, null);
            wv_option = (WheelView) view.findViewById(R.id.wv_option);
            wv_option_1 = (WheelView) view.findViewById(R.id.wv_option_1);
            wv_option_2 = (WheelView) view.findViewById(R.id.wv_option_2);
            wv_option.setAdapter(new ArrayWheelAdapter((ArrayList) list1));
            wv_option_1.setAdapter(new ArrayWheelAdapter((ArrayList) list2));
            wv_option_2.setAdapter(new ArrayWheelAdapter((ArrayList) list3));
            //是否循环
            wv_option.setCyclic(false);
            wv_option.setCurrentItem(0);
            wv_option.setTextSize(21);
            wv_option_1.setCurrentItem(0);
            wv_option_1.setCyclic(false);
            wv_option_1.setTextSize(21);
            wv_option_2.setCyclic(false);
            wv_option_2.setCurrentItem(0);
            wv_option_2.setTextSize(21);

            view.findViewById(R.id.view_up).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    dialog.dismiss();
//                    popupWindow.dismiss();
                }
            });

            view.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    dialog.dismiss();
//                    popupWindow.dismiss();
                }
            });

            view.findViewById(R.id.tv_submit).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    dialog.dismiss();
//                    popupWindow.dismiss();
                    threeClick.onThreeClick(view,wv_option.getCurrentItem(),wv_option_1.getCurrentItem(),wv_option_2.getCurrentItem());
                }
            });
        }

        dialog.setContentView(view);
        dialog.setDialogGravity(Gravity.BOTTOM);
        dialog.setWH(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

//        popupWindow.setContentView(view);
//        popupWindow.setOutsideTouchable(true);
//        popupWindow.setFocusable(true);
//        popupWindow.setBackgroundDrawable(new BitmapDrawable());
//        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
//        popupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
//        popupWindow.showAtLocation(((ViewGroup) ((Activity) context).findViewById(android.R.id.content)).getChildAt(0), Gravity.CENTER, 0, 0);
    }
}
